package com.fg.cloud.agency.controller.product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.agency.controller.BaseAgencyController;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.agency.AgencyFavoritesEntity;
import com.fg.cloud.manager.service.agency.AgencyFavoritesService;
import com.fg.cloud.manager.service.product.ProductInfoService;

/**
 * Description:商品收藏夹模块
 * @author dingjiang
 * @date 2017年8月18日上午10:03:16
 */
@Controller
@RequestMapping("/agencyFav")
public class AgencyFavoritesController extends BaseAgencyController {

	@Autowired
	private AgencyFavoritesService agencyFavoritesService;
	
	@Autowired
	private ProductInfoService productInfoService;
	
	/**
	 * Description:添加商品至收藏夹
	 * @param req
	 * @param resp
	 * @param agencyAccount
	 * @author dingjiang
	 * @date 2017年8月18日上午10:31:40
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/insert",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void insert(HttpServletRequest req, HttpServletResponse resp, AgencyFavoritesEntity agencyFav){
		try {
			/*ProductInfoVo entity = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(product), ProductInfoVo.class);*/
			/*AgencyFavoritesEntity agencyFav = new AgencyFavoritesEntity();*/
			//验证商品ID非空
			if ( LongUtils.isEmpty(agencyFav.getProId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "商品ID不能为空");
				return ;
			}
			//判断要添加的商品是否存在
			if ( !productInfoService.find(agencyFav.getProId(), getCurrentAgencyUser().getFacId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "该商品不存在");
				return ;
			}
			/*agencyFav.setProId(entity.getProId());*/
			/*agencyFav.setProductName(entity.getProductName());*/
			/*agencyFav.setProductImg(entity.getImg());*/
			agencyFav.setAgeId(getCurrentAgencyUser().getAgeId());
			agencyFav.setFacId(getCurrentAgencyUser().getFacId());
			//判断数据表里有没有重复的记录
			if ( agencyFavoritesService.selectByCondition(agencyFav) ) {
				doResult(resp, true, StatusCode.SAVESUCCESS, null, "商品已存在收藏夹中");
				return ;
			}
			agencyFavoritesService.insertSelective(agencyFav);
		    doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);	
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:经销商用户分页查看收藏夹
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @author dingjiang
	 * @date 2017年8月23日下午6:33:55
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/selectpage",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectPage(HttpServletRequest req, HttpServletResponse resp,
			Integer page, Integer rows, String sort, String order ){
		try {
			if (null == page) page = 1;
			if (null == rows) rows = 10;
			PageUtils<AgencyFavoritesEntity> pageinfo = new PageUtils<AgencyFavoritesEntity>(page, rows, sort, order);
			pageinfo.getCondition().put("facId", getCurrentAgencyUser().getFacId());
			pageinfo.getCondition().put("ageId", getCurrentAgencyUser().getAgeId());
			agencyFavoritesService.selectforPage(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
	
	
	/**
	 * Description:删除收藏夹商品
	 * @param req
	 * @param resp
	 * @author dingjiang
	 * @date 2017年8月18日下午2:44:01
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/delete",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void delete(HttpServletRequest req , HttpServletResponse resp , @Param("ids")String ids){
		try {
			String[] strs = ids.split(",");
			
			Long[] arrays = new Long[strs.length];
			for(int i = 0; i < strs.length; i++){
				arrays[i] = Long.parseLong(strs[i]);
		    }
		
			Long facId = getCurrentAgencyUser().getFacId();
			int flag = agencyFavoritesService.deletes(facId,arrays);
			if(flag > 0 ){
				doResult(resp, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			}else{
				doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
				return ;
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
		}
	}
	
}
